import yaml
import json
import csv
from openpyxl import load_workbook


# yaml读取
def read_yaml(filepath):
    with open(filepath, encoding='utf-8') as f:
        data = yaml.safe_load(f)
    return data


# json读取
def read_json(filepath):
    with open(filepath, 'r', encoding='utf-8') as f:
        data = json.load(f)
    return data


def read_csv(filepath):
    from httprunner.loader import load_csv_file

    # 使用HttpRunner内置的load_csv_file函数
    return load_csv_file(filepath)

# csv读取


def read_csv1(filepath):
    with open(filepath, 'r') as f:
        datas = csv.reader(f)
        lis = []
        for item in datas:
            lis.append(item)

        lis.pop(0)
        return lis


# xlsx读取
def read_xlsx(filepath, sheet_name):
    excel = load_workbook(filepath)
    sheet = excel[sheet_name]
    headers = [cell.value for cell in sheet[1]]
    data = []
    for row in sheet.iter_rows(min_row=2):
        row_data = {headers[i].strip(): cell.value for i, cell in enumerate(row)}
        data.append(row_data)
    return data
